package com.tnl.base.web;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tnl.base.Const;

public class JsonController {

	public int getPageNum(int totalCount,int pageSize){
		return totalCount%pageSize==0?totalCount/pageSize:totalCount / pageSize + 1;
	}
	/**
	 * �����Ƿ���POST
	 * @param request
	 * @return
	 */
	public boolean isPost(HttpServletRequest request){
		if(request.getMethod().equals("POST")){
			return true;
		}
		return false;
	}
	
	private HttpServletRequest getRequest(){
		return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	}
	
	public boolean isPost(){
		if(getRequest().getMethod().equals("POST")){
			return true;
		}
		return false;
	}
	
	public void successJson(Object data,String msg,HttpServletResponse response){
		responseJson(new JsonRes("200",data,msg),response);
	}
	
	public void errorJson(Object data,String msg,HttpServletResponse response){
		responseJson(new JsonRes("400",data,msg),response);
	}
	
	public void responseJson(Object obj,HttpServletResponse response){
		response.setContentType(Const.JSON_CONTENT_TYPE);
		response.setCharacterEncoding(Const.ENCODING);
		try 
		{
			PrintWriter writer=response.getWriter();
			ObjectMapper  objectMapper=new ObjectMapper();
			String result=objectMapper.writeValueAsString(obj);
			writer.write(result);
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String error(String msg){
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		request.setAttribute(Const.RESPONSE_MSG_KEY, msg);
		return Const.ERROR_PAGE;
	}
	
	public String toJson(Object obj){
		ObjectMapper objectMapper=new ObjectMapper();	
		try {
			return objectMapper.writeValueAsString(obj);
		} catch (JsonProcessingException e) {			
			e.printStackTrace();
		}
		return null;
	}
	
	public <T> T parseJson(String str,Class<T> type){
		ObjectMapper objectMapper=new ObjectMapper();
		try {
			return objectMapper.readValue(str, type);
		} catch (JsonParseException e) {
			
			e.printStackTrace();
		} catch (JsonMappingException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		return null;
	}
	
	class JsonRes{
		public JsonRes(String code,Object data,String msg){
			this.code=code;
			this.data=data;
			this.msg=msg;
		}
		private String code;
		
		private Object data;
		
		private String msg;

		public String getCode() {
			return code;
		}

		public void setCode(String code) {
			this.code = code;
		}

		public Object getData() {
			return data;
		}

		public void setData(Object data) {
			this.data = data;
		}

		public String getMsg() {
			return msg;
		}

		public void setMsg(String msg) {
			this.msg = msg;
		}
		
	}
	
}

